﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using System;
using UnityEngine.UI;
using System.Threading.Tasks;

namespace Miao.UI
{
    /// <summary>
    /// 一个可以进行道具置入，并自由拖拽的简单背包系统
    /// 需要分配Content，并且Content下设置Slot，Slot下的实际拖拽物绑定DragAndDropEvent
    /// </summary>
    [SourcesPath("Assets/Plugins/miao-framework/UIPrefab/Package/Package.prefab")]
    public class Package : PackageUIBase<PackageItem>
    {

        public GameObject itemIstantiate;
        /// <summary>
        /// 所有的插槽
        /// </summary>
        [NonSerialized]
        public PackageSlot[] slots;
        public int ptr
        {
            get
            {
                for(int i = 0; i < slots.Length; i++)
                {
                    if (slots[i].item == null)
                    {
                        return i;
                    }
                }
                return -1;//代表已满
            }
        }
        public override async Task OnCreate()
        {
            slots = new PackageSlot[Content.childCount];
            for (int i = 0; i < slots.Length; i++)
            {
                slots[i] = new PackageSlot();
            }
            await base.OnCreate();
        }

        public override DragAndDropEvent AddItem(PackageItem item)
        {
           
            int index = ptr;
            if (index != -1)
            {
                GameObject newItem = GameObject.Instantiate(itemIstantiate);
                Transform child = Content.GetChild(ptr);
                newItem.transform.SetParent(child, false);
                newItem.GetComponentInChildren<TMP_Text>().text = item.name;
                newItem.GetComponent<Image>().sprite = item.sprite;

                ///绑定拿取
                DragAndDropEvent dragAndDropEvent = newItem.GetComponent<DragAndDropEvent>();
                dragAndDropEvent.onBeginDrag += DragAndDropEvent_onBeginDrag;
                dragAndDropEvent.onEndDrag += Package_onEndDrag;
                dragAndDropEvent.Data = item;
                slots[ptr].item = dragAndDropEvent;
                return dragAndDropEvent;
            }
            return null;
        }

        private void DragAndDropEvent_onBeginDrag(DragAndDropEvent item, UnityEngine.EventSystems.PointerEventData pointerEvent)
        {
            item.rectTransform.SetParent(UIManager.mainCanvas.transform, true);
            //item.transform.SetParent(this.transform,true);//转移父类
        }

        /// <summary>
        /// 拖拽结束吸附格子
        /// </summary>
        /// <param name="arg1"></param>
        /// <param name="pointerEvent"></param>
        /// <exception cref="System.NotImplementedException"></exception>
        private void Package_onEndDrag(DragAndDropEvent item, UnityEngine.EventSystems.PointerEventData pointerEvent)
        {

            ///获得最近的对象
            int newIndex = Content.NearestChildrenIndex(item.transform.position);
            if (slots[newIndex].item == null)//如果选定新插槽等于空
            {
                slots[item.index].item = null;//清除旧插槽

                item.rectTransform.SetParent(Content.GetChild(newIndex));//转移到新类型上
                //item.index = newIndex;
                slots[newIndex].item = item;
                item.rectTransform.anchoredPosition = Vector3.zero;


            }
            else
            {
                ChangeItem(item.index,newIndex);

            }
        }

        public void ChangeItem(int index1, int index2)
        {

            //transform.GetChild(index1)
            DragAndDropEvent exchange = slots[index1].item;
            slots[index1].item = slots[index2].item;
            slots[index2].item = exchange;

            var slot1 = slots[index1].item.rectTransform;
            slot1.SetParent(Content.GetChild(index1),false);
            slots[index1].item.rectTransform.anchoredPosition = Vector3.zero;

            var slot2 = slots[index2].item.rectTransform;
            slot2.SetParent(Content.GetChild(index2), false);
            slots[index2].item.rectTransform.anchoredPosition = Vector3.zero;

        }
        public void RemoveItem(int index)
        {
            GameObject.Destroy(slots[index].item.gameObject);
            slots[index].item = null;
        }
        public void RemoveItem(DragAndDropEvent drag)
        {
            for(int i = 0; i < slots.Length; i++)
            {
               if(slots[i].item == drag)
                {
                    RemoveItem(i);
                    return;
                }
            }
        }

        public override void RemoveItem()
        {
            GameObject.Destroy(slots[ptr].item.gameObject);
            slots[ptr].item = null;
           
        }
        public override void ChangeSlot()
        {

        }

    }
}
